package model

import (
	"chitchat_http/util"
	"errors"
	"github.com/doujunyu/gogo/sql_aid"
	"os"
)

const TableByChatGroup = "self_chat_group"

// ChatGroupByLogoPath 组图片
const ChatGroupByLogoPath = "chat_group_by_logo"

// ChatGroupByInfo 获取单个群组信息
func ChatGroupByInfo(groupId int64) (map[string]interface{},error) {
	groupSql, args := sql_aid.PgTable(TableByChatGroup).Where("id = ?", groupId).ToSql()
	groupData, err := sql_aid.DataToMap(PgSql.Query(groupSql, args...))
	if err != nil {
		return nil,err
	}
	if len(groupData) == 0 {
		return nil,errors.New("空用户")
	}
	http := os.Getenv("SERVER_HTTP")
	if groupData[0]["logo"] != nil {
		groupData[0]["logo"] = util.IsPicturePath(groupData[0]["logo"].(string),http)
	}
	return groupData[0],nil
}

// ChatGroupToData 根据用户ID获取用户昵称和头像
func ChatGroupToData(groupIds ...interface{})map[int64]map[string]interface{}{
	userSql, args := sql_aid.PgTable(TableByChatGroup).Field("id", "name", "introduce","logo").WhereIn("id", groupIds...).ToSql()
	userdata, _ := sql_aid.DataToMap(PgSql.Query(userSql, args...))
	http := os.Getenv("SERVER_HTTP")
	for _, datum := range userdata {
		if datum["logo"] != nil {
			datum["logo"] = util.IsPicturePath(datum["logo"].(string),http)
		}
	}
	return util.ArrayColumnByInt64(&userdata, "id")
}

